package xxl.robotlegs.extensions.commandcenter.impl 
{
	import flash.utils.Dictionary;
	import xxl.robotlegs.extensions.commandcenter.api.ICommandTrigger;
	/**
	 * ...
	 * @author XXL
	 */
	public class CommandTriggerMap 
	{
		private const _triggers:Dictionary = new Dictionary();
		private var _keyFactory:Function;
		private var _triggerFactory:Function;
		
		public function CommandTriggerMap(keyFactory:Function, triggerFactory:Function) 
		{
			_keyFactory = keyFactory;
			_triggerFactory = triggerFactory;
		}
		
		public function getTrigger(...params):ICommandTrigger
		{
			const key:Object = getKey(params);
			return _triggers[key] ||= createTrigger(params);
		}
		
		public function removeTrigger(...params):ICommandTrigger
		{
			return destroyTrigger(getKey(params));
		}
		
		//----------private
		
		private function getKey(mapperArgs:Array):Object
		{
			return _keyFactory.apply(null, mapperArgs);
		}
		
		private function createTrigger(mapperArgs:Array):ICommandTrigger
		{
			return _triggerFactory.apply(null, mapperArgs);
		}
		
		private function destroyTrigger(key:Object):ICommandTrigger
		{
			const trigger:ICommandTrigger = _triggers[key];
			if (trigger)
			{
				trigger.deactivate();
				delete _triggers[key];
			}
			return trigger;
		}
	}

}